package cn.nuist.service.impl;

import cn.nuist.dao.BillDao;
import cn.nuist.dao.CustomerDao;
import cn.nuist.dao.ProductDao;
import cn.nuist.dao.SupplierDao;
import cn.nuist.entity.*;
import cn.nuist.entity.enu.OrderStatus;
import cn.nuist.entity.enu.PaymentMethod;
import cn.nuist.service.BillService;
import cn.nuist.utils.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

@Service
public class BillServiceImpl implements BillService {
    private BillDao billDao;
    private CustomerDao customerDao;

    private ProductDao productDao;
    private SupplierDao supplierDao;


    @Autowired
    public BillServiceImpl(BillDao billDao, CustomerDao customerDao, ProductDao productDao, SupplierDao supplierDao) {
        this.billDao = billDao;
        this.customerDao = customerDao;
        this.productDao = productDao;
        this.supplierDao = supplierDao;
    }

    @Override
    public Page<OrderSummaryView> getViewPage(Integer pageNum) {
        Integer count = billDao.getCount();
        Page<OrderSummaryView> page = new Page<>();
        List<OrderSummaryView> list = billDao.getByPage((pageNum - 1) * page.getPageSize(),page.getPageSize());
        page.setList(list);
        page.setTotalCount(count);
        page.setCurrentPage(pageNum);
        return page;
    }

    @Override
    public List<OrderItemsView> getdetail(Integer orderID) {
        return billDao.getdetail(orderID);
    }

    @Override
    public Page<OrderSummaryView> getBillByName(Integer pageNum, String cutomerName) {
        Integer count = billDao.getCountByName(cutomerName);
        Page<OrderSummaryView> page = new Page<>();
        List<OrderSummaryView> list = billDao.getBillByName((pageNum - 1) * page.getPageSize(),page.getPageSize(),cutomerName);
        page.setList(list);
        page.setTotalCount(count);
        page.setCurrentPage(pageNum);
        return page;
    }

    @Override
    public void add(Model model) {
        model.addAttribute("customers",customerDao.getAllCustomer());
        model.addAttribute("products",productDao.getAll());
        model.addAttribute("suppliers",supplierDao.getAllSupplier());
    }

    @Override
    public void saveOrder(String customerName,
                          String[] productNames,
                          int[] quantities,
                          PaymentMethod paymentMethod,
                          OrderStatus orderStatus,
                          double[] discountRate) {
        Customer customerByName = customerDao.getCustomerByName(customerName);
        int id = customerByName.getCustomerID();

        Orders orders = new Orders(id,LocalDateTime.now(),orderStatus,paymentMethod);
        billDao.saveOrders(orders);
        int orderID = billDao.getCount()+1;
        for (int i = 0; i < productNames.length; i++) {
            Products productByName = productDao.getProductByName(productNames[i]);
            int productID = productByName.getProductID();
            int quantity = quantities[i];
            double unitPrice = productByName.getUnitPrice();
            double discountR = discountRate[i];
            double lineTotal = unitPrice*quantity;
            OrderItems orderItems = new OrderItems(orderID,productID,quantity,unitPrice,discountR,lineTotal);
            billDao.saveOrderItems(orderItems);
        }
    }
}
